/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.DeliverBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.PageHelper;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class DeliverBillItemDaoImpl extends BaseDaoImpl
		implements
			DeliverBillItemDao {
	public List getListByDeliverBill(long billId) {
		String sql = "from com.minierp.model.DeliverBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List getListByBusinessBill(long billId) {
		String sql = "select b,s,bbItem from com.minierp.model.DeliverBill b ,com.minierp.model.DeliverBillItem s ,com.minierp.model.BusinessBillItem bbItem where b.id=s.billId and s.bbItemId=bbItem.id and b.enabled=\'Y\' and b.businessBillId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.DeliverBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteByDeliverBillId(final long billId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.DeliverBillItem where billId=:billId";
						return new Integer(session.createQuery(hql)
								.setLong("billId", billId).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public BigDecimal getDeliveredAmount(long bbItemId) {
		String sql = "select sum(i.amount) from com.minierp.model.DeliverBill b ,com.minierp.model.DeliverBillItem i where b.id=i.billId and i.bbItemId="
				+ bbItemId + " and b.enabled=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 && list.get(0) != null ? (BigDecimal) list
				.get(0) : new BigDecimal(0);
	}

	public List getListForPrintMulti(String[] idArr) {
		if (idArr != null && idArr.length > 0) {
			String sql = "select b.linkman,b.phone,b.address,b.creater_name,bi.make_type_id,bi.pro_name,bi.finished_content,bi.specification,bi.amount as amount_total,i.amount,bi.unit_price,0,0,bi.unit,bi.square_num,bi.price_type,bb.bill_code,bi.leng_type,bb.client_id,e.est_name,e.deli_pphone,b.deliver_date from deliver_bill_item i left join deliver_bill b on b.id=i.bill_id left join business_bill_item bi on bi.id=i.bb_item_id left join business_bill bb on bb.id=bi.bill_id left join establishment e on bb.client_id=e.id where "
					+ PageHelper.createIdArrStr(idArr, "b.id", false)
					+ " order by e.id,b.id,i.id";
			Session sess = null;

			List arg5;
			try {
				sess = this.getSession();
				SQLQuery q = sess.createSQLQuery(sql);
				arg5 = q.list();
			} finally {
				if (sess != null) {
					this.releaseSession(sess);
				}

			}

			return arg5;
		} else {
			return new ArrayList();
		}
	}
}